﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runa.Common.Helper;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.IO;
using System.Collections;

namespace Runa.Build.Tasks
{

    public class ZipPackaging
        : Task
    {
        [Required]
        public string Output { get; private set; }

        [Required]
        public ITaskItem[] InputItems { get; private set; }

        [Output]
        public ITaskItem[] ItemsAdded { get; private set; }

        public override bool Execute()
        {
            try
            {
                var lst = new List<string>();
                var input = new List<FileInfo>();
                foreach (ITaskItem inputItem in InputItems)
                {
                    var item = new FileInfo(inputItem.ItemSpec);
                    lst.Add(item.FullName);
                }
                Runa.Common.Helper.ModuleIO.MakeZipFile(this.Output, 9, lst.ToArray());
                ItemsAdded = lst.ToArray((obj) => new TaskItem(obj));
                return true;
            }
            catch (Exception e)
            {
                Log.LogError("Packaging error: {0} with \n{1}.", e.Message, e.StackTrace.ToString());
                return false;
            }
        }

    }

}